package main

import (
	"encoding/hex"
	"fmt"
	"net"
	"time" // 引入 time 包
)

func main() {
	// 服务器地址和端口
	serverAddr := "localhost:8080"

	// 待发送的十六进制数据字符串（注意去除空格）
	hexData := "a55a6700100248c57d0a3839383630343636313231394330323939313439000000000200000000000000000000157c00000708000001f400000fa00000000000000000000000000000000000001356000000320000003200000320000030d45a646ea3cc33c33c"
	hexData_1 := "a55a4100100648c57c876684c1ea00002d210000039cffffb4c5000000030000012201000000200000000000000000000000000000000000000001be65cc33c33c"
	hexData_2 := "a55a5d00100848c57d1d6684c2996684c2546684c2990000001c0b00000556140000cdae0000d2f000000c36000013850000138a00000b8a000009f600000a8000000b82000009f60000000000000000000000000000000099cc33c33c"
	hexData_3 := "a55a4100100648c57d1c6684c1b200000fa000000ec5ffff2d1000000088000000bb480000141e4400000000000000000000000000020000000000647dcc33c33c"
	// 将十六进制字符串解码为字节数组
	rawData, err := hex.DecodeString(hexData)
	if err != nil {
		fmt.Println("Error decoding hex data:", err)
		return
	}
	rawData_1, err := hex.DecodeString(hexData_1)
	if err != nil {
		fmt.Println("Error decoding hex data:", err)
		return
	}
	rawData_2, err := hex.DecodeString(hexData_2)
	if err != nil {
		fmt.Println("Error decoding hex data:", err)
		return
	}
	rawData_3, err := hex.DecodeString(hexData_3)
	if err != nil {
		fmt.Println("Error decoding hex data:", err)
		return
	}

	// 建立 TCP 连接
	conn, err := net.Dial("tcp", serverAddr)
	if err != nil {
		fmt.Println("Error connecting to server:", err)
		return
	}
	defer conn.Close()

	//发送数据函数
	sendData := func() {
		_, err := conn.Write(rawData)
		if err != nil {
			fmt.Println("Error sending data:", err)
			return
		}
		fmt.Println("Data sent successfully.")
	}
	//首次立即发送数据
	sendData()
	time.Sleep(3 * time.Second)

	sendData_1 := func() {
		_, err := conn.Write(rawData_1)
		if err != nil {
			fmt.Println("Error sending data:", err)
			return
		}
		fmt.Println("Data sent successfully.")
	}
	sendData_1()
	time.Sleep(3 * time.Second)
	sendData_2 := func() {
		_, err := conn.Write(rawData_2)
		if err != nil {
			fmt.Println("Error sending data:", err)
			return
		}
		fmt.Println("Data sent successfully.")
	}
	sendData_2()
	time.Sleep(3 * time.Second)
	sendData_3 := func() {
		_, err := conn.Write(rawData_3)
		if err != nil {
			fmt.Println("Error sending data:", err)
			return
		}
		fmt.Println("Data sent successfully.")
	}
	sendData_3()

	// 定时器，每隔 30 秒发送一次数据
	ticker := time.NewTicker(15 * time.Second)
	defer ticker.Stop()

	for range ticker.C {
		sendData()
	}
}
